package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityGoodsOrderCustomerDAO;
import com.ecoolex.ying.space.core.service.SmsService;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomer;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomerExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomer.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomerExample.Criteria;
import com.ecoolex.ying.space.pojo.UpdateEstateProjectActivityGoodsOrderCustomerDTO;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityGoodsOrderCustomerManager {
	
    @Autowired
    private EstateProjectActivityGoodsOrderCustomerDAO dao;
    
    @Autowired
    private EstateProjectActivityGoodsOrderManager orderManager;
    
    @Autowired
    private SmsService smsService;
    
    public EstateProjectActivityGoodsOrderCustomer addCustomer(EstateProjectActivityGoodsOrderCustomer record, String createUser) {
    	EstateProjectActivityGoodsOrderCustomer old = getByOrderId(record.getOrderId());
    	BizAssert.isTrue(Check.isNull(old),ResultCode.VERIFY.build(10), "此订单已经存在客户信息");
    	record.setDeleted(EstateProjectActivityGoodsOrderCustomer.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		//发送短信提示订单信息
		EstateProjectActivityGoodsOrder order = orderManager.get(record.getOrderId());
		smsService.sendEstateProjectActivityGoodsOrderSms(record.getName(),order.getOrderNo(),"86", record.getPhone());
    	return record;
	}

	public EstateProjectActivityGoodsOrderCustomer editCustomer(EstateProjectActivityGoodsOrderCustomer record, String updateUser) {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.orderId,Column.deleted,Column.createUser,Column.createTime));
		//发送短信提示订单信息
		EstateProjectActivityGoodsOrder order = orderManager.get(record.getOrderId());
		smsService.sendEstateProjectActivityGoodsOrderSms(record.getName(),order.getOrderNo(),"86", record.getPhone());
		return record;
	}
	
	public EstateProjectActivityGoodsOrderCustomer updateCustomer(UpdateEstateProjectActivityGoodsOrderCustomerDTO record, String updateUser) throws BizException {
		EstateProjectActivityGoodsOrderCustomer old = getByOrderId(record.getOrderId());
		if(Check.notNull(old)) {
			old.setName(record.getName());
			old.setAddress(record.getAddress());
			old.setPhone(record.getPhone());
			old.setRemark(record.getRemark());
			old.setUpdateUser(updateUser);
			old.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(old, Column.excludes(Column.deleted,Column.orderId,Column.createUser,Column.createTime));
			//发送短信提示订单信息
			EstateProjectActivityGoodsOrder order = orderManager.get(record.getOrderId());
			smsService.sendEstateProjectActivityGoodsOrderSms(record.getName(),order.getOrderNo(),"86", record.getPhone());
			return old;
		}else {
			EstateProjectActivityGoodsOrderCustomer customer = new EstateProjectActivityGoodsOrderCustomer();
			customer.setOrderId(record.getOrderId());
			customer.setName(record.getName());
			customer.setAddress(record.getAddress());
			customer.setPhone(record.getPhone());
			customer.setRemark(record.getRemark());
			customer.setDeleted(EstateProjectActivityGoodsOrderCustomer.NOT_DELETED);
			customer.setCreateUser(updateUser);
			customer.setCreateTime(Date.from(Instant.now()));
			customer.setUpdateUser(updateUser);
			customer.setUpdateTime(Date.from(Instant.now()));
			dao.insert(customer);
			//发送短信提示订单信息
			EstateProjectActivityGoodsOrder order = orderManager.get(record.getOrderId());
			smsService.sendEstateProjectActivityGoodsOrderSms(customer.getName(),order.getOrderNo(),"86", customer.getPhone());
			return customer;
		}
	}
	
	public EstateProjectActivityGoodsOrderCustomer getCustomer(Integer id) {
		return dao.selectByPrimaryKeyWithLogicalDelete(id, EstateProjectActivityGoodsOrderCustomer.NOT_DELETED);
	}
	
	public EstateProjectActivityGoodsOrderCustomer getByOrderId(Integer orderId) {
		EstateProjectActivityGoodsOrderCustomerExample example = new EstateProjectActivityGoodsOrderCustomerExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(EstateProjectActivityGoodsOrderCustomer.NOT_DELETED);
		return dao.selectOneByExample(example);
	}
}